package forkjoin;

import java.util.concurrent.RecursiveTask;

public class ForkJoinCount extends RecursiveTask<Long> {

    private int start;
    private int end;

    public ForkJoinCount(int start, int end) {
        this.start = start;
        this.end = end;
    }

    //临界值
    private int temp = 10_0000;

    @Override
    protected Long compute() {
        if((end-start)>temp){
            //forkjoin计算

            //中间值
            int middle = (start + end)/2;
            ForkJoinCount task1 = new ForkJoinCount(start,middle);
            //把任务压入线程队列
            task1.fork();
            ForkJoinCount task2 = new ForkJoinCount(middle+1,end);
            task2.fork();

            return task1.join()+task2.join();
        }else {
            long sum = 0 ;
            for(int i = start; i <= end ;  i++){
                sum+=i;
            }
            return sum;
        }
    }
}
